package com.zdb.demo.design.pattern.structural.proxy.staticproxy;

import com.zdb.demo.design.pattern.structural.proxy.IOrderService;
import com.zdb.demo.design.pattern.structural.proxy.Order;
import com.zdb.demo.design.pattern.structural.proxy.OrderDaoImpl;
import com.zdb.demo.design.pattern.structural.proxy.OrderServiceImpl;
import com.zdb.demo.design.pattern.structural.proxy.db.DataSourceContextHolder;

public class OrderServiceStaticProxy {

    public int saveOrder(Order order) {
        beforeMethod();

        IOrderService orderService = new OrderServiceImpl(new OrderDaoImpl());

        int dbType = order.getId() % 2;

        // todo 设置数据库
        DataSourceContextHolder.setDbType("db" + dbType);
        System.out.println("--- 静态代理，选择[db" + dbType + "],orderId:" + order.getId());
        int result = orderService.saveOrder(order);

        afterMethod();

        return result;
    }

    private void beforeMethod() {
        System.out.println("静态代理 before code");
    }
    private void afterMethod() {
        System.out.println("静态代理 after code");
    }
}
